﻿/*
	wp8libflac project
	© Alovchin, 2014
*/

using System;
using Microsoft.Phone.BackgroundAudio;
using wp8.MediaHelpers;

namespace wp8.StreamAgent
{
    /// <summary>
    /// A background agent that performs per-track streaming for playback
    /// </summary>
    public class AudioTrackStreamer : AudioStreamingAgent
    {
        private MediaStreamSourceBase _mediaStreamSource;

        /// <summary>
        /// Called when a new track requires audio decoding
        /// (typically because it is about to start playing)
        /// </summary>
        /// <param name="track">
        /// The track that needs audio streaming
        /// </param>
        /// <param name="streamer">
        /// The AudioStreamer object to which a MediaStreamSource should be
        /// attached to commence playback
        /// </param>
        /// <remarks>
        /// To invoke this method for a track set the Source parameter of the AudioTrack to null
        /// before setting  into the Track property of the BackgroundAudioPlayer instance
        /// property set to true;
        /// otherwise it is assumed that the system will perform all streaming
        /// and decoding
        /// </remarks>
        protected override void OnBeginStreaming(AudioTrack track, AudioStreamer streamer)
        {
            if (this._mediaStreamSource != null)
            {
                this._mediaStreamSource.Dispose();
            }

            this._mediaStreamSource = StreamSourceFactory.Create(track.Tag);
            this._mediaStreamSource.StreamComplete += this.MediaStreamSourceOnStreamComplete;
            streamer.SetSource(this._mediaStreamSource);
        }

        private void MediaStreamSourceOnStreamComplete(object sender, EventArgs eventArgs)
        {
            this._mediaStreamSource.StreamComplete -= this.MediaStreamSourceOnStreamComplete;
            NotifyComplete();
        }

        /// <summary>
        /// Called when the agent request is getting cancelled
        /// The call to base.OnCancel() is necessary to release the background streaming resources
        /// </summary>
        protected override void OnCancel()
        {
            base.OnCancel();

            if (this._mediaStreamSource != null)
            {
                this._mediaStreamSource.StreamComplete -= this.MediaStreamSourceOnStreamComplete;
                this._mediaStreamSource.Dispose();
            }
        }
    }
}
